package com.iims.market.service.impl;

import cn.hutool.core.lang.Validator;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.json.JSONUtil;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.iims.common.exception.CustomException;
import com.iims.common.utils.SeqNumUtils;
import com.iims.common.vo.CreateUseInfo;
import com.iims.market.domain.PurposeContract;
import com.iims.market.mapper.PurposeContractMapper;
import com.iims.market.service.IContractCommonService;
import com.iims.market.service.IPurposeContractService;
import com.iims.market.vo.ContractReqInfoVo;
import com.iims.market.vo.GroupPurposeContractSearchQuery;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 意向合同服务
 *
 * @author fanpei
 * @date 2021/6/17 15:09
 */
@Service
public class PurposeContractServiceImpl extends ServiceImpl<PurposeContractMapper, PurposeContract> implements IPurposeContractService {

    @Autowired
    private IContractCommonService commonService;

    @Override
    public List<PurposeContract> selectPContractList(PurposeContract pContract) {
        List list= baseMapper.selectPContractList(pContract);
        return SeqNumUtils.initSeqNum(pContract, list);
    }

    @Override
    public List<PurposeContract> selectPContractListGroup(GroupPurposeContractSearchQuery searchQuery) {
        searchQuery.generateSqlArgs();
        List list= baseMapper.selectPContractListByGroup(searchQuery);
        return SeqNumUtils.initSeqNum(searchQuery, list);
    }

    @Override
    public PurposeContract selectDetailById(Long pContractId) {
        PurposeContract pContract = baseMapper.selectPContractDetailById(pContractId);
        if (Validator.isNotNull(pContract)) {
            String extenions = baseMapper.selectPContractExtentions(pContractId);
            if (Validator.isNotEmpty(extenions)) {
                pContract.setContractReqInfo(JSONUtil.toBean(extenions, ContractReqInfoVo.class));
            }
        }
        return pContract;
    }

    @Override
    public int insertPContract(PurposeContract pContract) {
        commonService.checkNameAndCodeUnique(
                pContract.getPurposeContractId(), pContract.getContractName(), pContract.getPcontractCode());
        //转换为json
        pContract.setExtentions(JSONUtil.toJsonStr(pContract.getContractReqInfo()));
        return baseMapper.insert(pContract);
    }

    @Override
    public String createFlowInfo(PurposeContract pContract, CreateUseInfo createUseInfo) {
        if (ObjectUtil.isEmpty(pContract.getPurposeContractId())) {
            pContract.setCreateBy(createUseInfo.getCreateBy());
            pContract.setCreateTime(createUseInfo.getCreateTime());
            baseMapper.insert(pContract);
        } else {
            pContract.setUpdateBy(createUseInfo.getCreateBy());
            pContract.setUpdateTime(createUseInfo.getCreateTime());
            baseMapper.updateById(pContract);
        }
        return pContract.getPcontractCode();
    }

    @Override
    public int updatePContract(PurposeContract pContract) {
        commonService.checkNameAndCodeUnique(
                pContract.getPurposeContractId(), pContract.getContractName(), pContract.getPcontractCode());
        //转换为json
        pContract.setExtentions(JSONUtil.toJsonStr(pContract.getContractReqInfo()));
        return baseMapper.updateById(pContract);
    }


    @Override
    public int deletePContractById(Long pContractId) {
        if (Validator.isNotEmpty(baseMapper.selectPContractOfficialCode(pContractId))) {
            throw new CustomException("合同id" + pContractId + "的合同已转为正式合同，无法删除");
        }
        return baseMapper.deleteById(pContractId);
    }
}
